import greenfoot.*;
import java.util.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Welt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Welt extends World
{
    Normal roman;
    Trip trip;
    Trap trap;
    public int linkerRand ;
    public int rechterRand ;
    Lebensbalken tripLeb ;
    Lebensbalken trapLeb ;
    List<Fliesband> flies;
    List<Eisplat> eis;
    List<Normal> normal;

    public GreenfootImage bg = new GreenfootImage(getBackground());
    int posX = 0;
    /**
     * Constructor for objects of class Welt.
     * 
     */
    public Welt()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        //
        super(1024, 720, 1,false); 
        trip = new Trip();
        trap = new Trap();
        addObject(trip, 450, 581);
        addObject(trap, 550, 581);
        linkerRand = 0;
        rechterRand = 1024;
        tripLeb = new Lebensbalken("Trip");
        trapLeb = new Lebensbalken("Trap");
        addObject(tripLeb, 100, 20);
        addObject(trapLeb, 924, 20);
        for (int i = 1; i < 51; i++)
        {
            addObject(new Normal(20,200,0),i*200-100,710);
        }
        for (int i = 1; i < 11; i++)
        {
            addObject(new Panzer(1), i*1000, 600);
        }
        for (int i = 1; i < 6; i++)
        {
            addObject(new Schnell(1), i*2000, 600);
        }
        for (int i = 1; i < 9; i++)
        {
            addObject(new Drohne(1), i*1200, 50);
        }
        for (int i = 1; i < 9; i++)
        {
            addObject(new Fliesband(39 , 200 , 0),i*Greenfoot.getRandomNumber(3)*500, 500);
        }
        prepare();

    }

    public void act() {
        positioning();

    }

      
    public int linksRand()
    {
        return linkerRand;
    }

    public int rechtsRand()
    {
        return rechterRand;
    }

    public void tripLebZeichnen(int z)
    {
        tripLeb.zeichnen(z);
    }

    public void trapLebZeichnen(int z)
    {
        trapLeb.zeichnen(z);
    }

    public void scrollRight(){
        if(rechterRand <= bg.getWidth()-10){
            for(Object a: getObjects(ScrollableActor.class)){

                ((ScrollableActor)a).setLocation(((ScrollableActor)a).getX() - 10,((ScrollableActor)a).getY());

            }

            getBackground().clear();
            if(posX != 1650 ){
                posX-= 10;
                linkerRand = linkerRand + 10;
                rechterRand = rechterRand + 10;
                getBackground().drawImage(bg, posX, 0);  

            }
        }
        else
        {
            return;
        }
    }

    public void scrollLeft(){
        if(linkerRand >= 10) {
            for(Object a: getObjects(ScrollableActor.class)){

                ((ScrollableActor)a).setLocation(((ScrollableActor)a).getX() + 10,((ScrollableActor)a).getY());

            } 

            getBackground().clear();
            if(posX != 0){
                posX+= 10;
                linkerRand = linkerRand - 10;
                rechterRand = rechterRand - 10;
                getBackground().drawImage(bg, posX, 0); 
            }
        }
        else
        {
            return;
        }
    }

    public Trip getTrip() {
        return trip;
    }

    public Trap getTrap() {
        return trap;
    }

    public void positioning() {

        if (trip==null) trip = getTrip(); if (trip==null) return;
        if (trap==null) trap = getTrap(); if (trap==null) return;

        double x=(trip.getX()+trap.getX())/2 - 512;
        if( x > -50 && x < 50)
        {
            return;
        }
        if(x > 0) {
            scrollRight();
        }
        else {
            scrollLeft();

        }

    }

    List <Plattform> plattformListe() //gibt eine Liste aller Plattformen zurück
    {
        return getObjects(Plattform.class);
    }

    List <Gegner> gegnerListe()// gibt eine Liste aller Gegner zurück
    {
        return getObjects(Gegner.class);
    }

    List <Upgrade> upgradeListe()// gibt eine Liste aller Upgrades zurück
    {
        return getObjects(Upgrade.class);
    }

    public void save(){
        for(Plattform a: plattformListe()){
            System.out.println("plattform p1 = new Plattform();");
            System.out.println("addObject(p1,"+a.getX()+","+a.getY()+")");

        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {

        Fliesband fliesband = new Fliesband(39, 200, 0);
        addObject(fliesband, 636, 575);
        fliesband.setLocation(688, 532);
        fliesband.setLocation(682, 555);
        Fliesband fliesband2 = new Fliesband(39, 200, 2);
        addObject(fliesband2, 869, 442);
        fliesband2.setLocation(897, 454);
    }
}